package views;

import views.panels.PanelGeneral;
import java.awt.BorderLayout;
import javax.swing.DefaultComboBoxModel;
import java.util.List;
import logicBusiness.AreaBL;
import logicBusiness.AsignaturaBL;
import models.Asignatura;
import javax.swing.JOptionPane;

/**
 *
 * @author JARR
 */
public class InterfazCrearAsignatura extends javax.swing.JFrame {

    private AreaBL areaBo = new AreaBL();
    private Integer ok;
    private AsignaturaBL asignaturaBo = new AsignaturaBL();

    /** Creates new form InterfazCrearAsignatura */
    public InterfazCrearAsignatura() {
        initComponents();
        PanelGeneral panel = new PanelGeneral();
        this.add(panel, BorderLayout.SOUTH);
        cargarListaGrados();
        cargarListaAreas();
        cargarListaIntensidadHoraria();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        CTNombreAsignatura = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        CBGrados = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        CBAreas = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        CBIntensidadHoraria = new javax.swing.JComboBox();
        BCrearAsignaturas = new javax.swing.JButton();
        BCancelar = new javax.swing.JButton();

        setResizable(false);

        jLabel1.setText("Nombre de la asignatura:");

        jLabel2.setText("Grado al que se dicta:");

        CBGrados.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel3.setText("Área:");

        CBAreas.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        CBAreas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CBAreasActionPerformed(evt);
            }
        });

        jLabel4.setText("Intensidad Horaria (en horas a la semana)");

        CBIntensidadHoraria.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        CBIntensidadHoraria.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CBIntensidadHorariaActionPerformed(evt);
            }
        });

        BCrearAsignaturas.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        BCrearAsignaturas.setText("Crear Asignatura");
        BCrearAsignaturas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BCrearAsignaturasActionPerformed(evt);
            }
        });

        BCancelar.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        BCancelar.setText("Cancelar");
        BCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(103, 103, 103)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jLabel3)
                                .addComponent(jLabel2)
                                .addComponent(jLabel1))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(CBAreas, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(CBGrados, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(CTNombreAsignatura, javax.swing.GroupLayout.DEFAULT_SIZE, 261, Short.MAX_VALUE)))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(25, 25, 25)
                            .addComponent(jLabel4)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(CBIntensidadHoraria, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(58, 58, 58)
                        .addComponent(BCrearAsignaturas, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(48, 48, 48)
                        .addComponent(BCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(34, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(37, 37, 37)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(CTNombreAsignatura, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(CBGrados, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(CBAreas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(CBIntensidadHoraria, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 41, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(BCrearAsignaturas)
                    .addComponent(BCancelar))
                .addGap(33, 33, 33))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void BCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BCancelarActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
    }//GEN-LAST:event_BCancelarActionPerformed

    private void BCrearAsignaturasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BCrearAsignaturasActionPerformed
        ok = 0;
        Asignatura tst;

        if (CTNombreAsignatura.getText().equals("") || CBIntensidadHoraria.getSelectedIndex() == 0 || CBGrados.getSelectedIndex() == 0) {
            JOptionPane.showMessageDialog(this, "Debe digitar un nombre, seleccionar una Intensidad Horaria y escoger un grado", null, JOptionPane.ERROR_MESSAGE);
        } else {
            tst = asignaturaBo.consultarAsignatura(CTNombreAsignatura.getText() + "", Integer.parseInt(CBGrados.getSelectedIndex() + ""));

            if (tst == null) {
                ok = asignaturaBo.crearAsignatura(null, CTNombreAsignatura.getText(), CBIntensidadHoraria.getSelectedIndex(), CBGrados.getSelectedIndex() - 1, areaBo.encontrarArea(CBAreas.getSelectedItem() + ""), null);
                CTNombreAsignatura.setText("");
            }
            if (ok == 1) {
                JOptionPane.showMessageDialog(this, "La creación de la asignatura fue exitosa", "Creación exitosa !!!", JOptionPane.INFORMATION_MESSAGE);
            } else {
                if (tst != null) {
                    JOptionPane.showMessageDialog(this, "Ocurrio un error al crear la asignatura (Nombres repetidos)", "Creación fallida", JOptionPane.ERROR_MESSAGE);
                } else {
                    JOptionPane.showMessageDialog(this, "Ocurrio un error al crear la asignatura", "Creación fallida", JOptionPane.ERROR_MESSAGE);
                }
            }
            // this.setVisible(false);t
            cargarListaAreas();
        }


    }//GEN-LAST:event_BCrearAsignaturasActionPerformed

    private void CBIntensidadHorariaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CBIntensidadHorariaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_CBIntensidadHorariaActionPerformed

    private void CBAreasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CBAreasActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_CBAreasActionPerformed

    private void cargarListaGrados() {
        DefaultComboBoxModel modelo = (DefaultComboBoxModel) CBGrados.getModel();
        modelo.removeAllElements();
        modelo.addElement("Seleccione");
        modelo.addElement("0.  Transicion");
        modelo.addElement("1.  Primero");
        modelo.addElement("2.  Segundo");
        modelo.addElement("3.  Tercero");
        modelo.addElement("4.  Cuarto");
        modelo.addElement("5.  Quinto");
        modelo.addElement("6.  Sexto");
        modelo.addElement("7.  Séptimo");
        modelo.addElement("8.  Octavo");
        modelo.addElement("9.  Noveno");
        modelo.addElement("10. Decimo");
        modelo.addElement("11. Once");
    }

    private void cargarListaAreas() {
        List l = areaBo.consultarAreas();
        DefaultComboBoxModel modelo = (DefaultComboBoxModel) CBAreas.getModel();
        modelo.removeAllElements();
        for (int i = 0; i < l.size(); i++) {
            modelo.addElement(l.get(i));

        }
        CBAreas.setModel(modelo);
    }

    private void cargarListaIntensidadHoraria() {
        DefaultComboBoxModel modelo = (DefaultComboBoxModel) CBIntensidadHoraria.getModel();
        modelo.removeAllElements();
        modelo.addElement("Seleccione");
        modelo.addElement("1 Hora");
        modelo.addElement("2 Horas");
        modelo.addElement("3 Horas");
        modelo.addElement("4 Horas");
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new InterfazCrearAsignatura().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BCancelar;
    private javax.swing.JButton BCrearAsignaturas;
    private javax.swing.JComboBox CBAreas;
    private javax.swing.JComboBox CBGrados;
    private javax.swing.JComboBox CBIntensidadHoraria;
    private javax.swing.JTextField CTNombreAsignatura;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    // End of variables declaration//GEN-END:variables
}
